﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileBinding.cs" company="">
//   
// </copyright>
// <summary>
//   The file binding.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.IO;

namespace Libium.Bindings
{
    using Libium.UseCases;
    using Libium.Views;

    /// <summary>
    /// The file binding.
    /// </summary>
    /// <typeparam name="T1">
    /// </typeparam>
    /// <typeparam name="T2">
    /// </typeparam>
    public class FileBinding<T1, T2> : UseCaseBinding<T1, T2>
        where T1 : IFileUseCase where T2 : IFileView
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FileBinding{T1,T2}"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        /// <param name="view">
        /// The view. 
        /// </param>
        public FileBinding(IBindService service, T1 useCase, T2 view)
            : base(service, useCase, view)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The bind.
        /// </summary>
        public override void Bind()
        {
            base.Bind();

            Bind(UseCase.BrowseCommand, View.BrowseCommandView);
            Bind(UseCase.SaveAsCommand, View.SaveAsCommandView);
				//Currently this option is not usefull. But maybe usefull in future.
            //Bind(UseCase.RefreshCommand, View.RefreshCommandView);
            Bind(UseCase.EditCommand, View.EditCommandView);
            Bind(UseCase.ClearCommand, View.ClearCommandView);

            //TODO Remove
            try
            {
                View.FilePath = UseCase.FilePath;
                View.Text = UseCase.Text;
                View.DetailsText = UseCase.DetailsText;
            }
            catch (ArgumentException)
            {
            }
            catch (InvalidOperationException)
            {
            }
            catch (FileNotFoundException)
            {
            }

            UseCase.FileChanged += OnFileChanged;
        }

        /// <summary>
        ///   The unbind.
        /// </summary>
        public override void Unbind()
        {
            base.Unbind();
            UseCase.FileChanged -= OnFileChanged;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on file changed.
        /// </summary>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        private void OnFileChanged(IFileUseCase useCase)
        {
            //TODO Remove
            try
            {
                View.FilePath = UseCase.FilePath;
                View.Text = UseCase.Text;
                View.DetailsText = UseCase.DetailsText;
            }
            catch (ArgumentException)
            {
            }
            catch (InvalidOperationException)
            {
            }
            catch (FileNotFoundException)
            {
            }
        }

        #endregion
    }

    /// <summary>
    ///   The file binding.
    /// </summary>
    public class FileBinding : FileBinding<IFileUseCase, IFileView>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FileBinding"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        /// <param name="view">
        /// The view. 
        /// </param>
        public FileBinding(IBindService service, IFileUseCase useCase, IFileView view)
            : base(service, useCase, view)
        {
        }

        #endregion
    }
}